<?php

namespace App\Http\Controllers\admin;

use App\Common\Responses;
use App\Http\Controllers\Controller;
use App\Models\lak\Login;
use App\Models\lak\Residential;
use App\Utils\Lib\Mortgage\MortgageFacaory;
use App\Utils\Lib\Mortgage\MortgageHandler;
use App\Utils\Lib\MyEs;
use App\Utils\Lib\Redis;
use App\Utils\Logic\lak\EsMultiple;
use App\Utils\Logic\lak\RedisRankHistoryLogic;
use App\Utils\Logic\lak\RentingLogic;
use Illuminate\Http\Request;
use OSS\OssClient;
use OSS\Core\OssException;
use App\Models\lak\Reservation;
class RentingController extends Controller
{
    /*
     * 房贷计算
     * @name lak
     * */
    public function Mortgage(Request $request)
    {
        try {
            $money = $request->post('money');
            $type = $request->post('type');
            $res = MortgageFacaory::MortgageSelection($type);
            $handler = (new MortgageHandler($res))->MakeMortgageInterface($money);
            return Responses::success($handler, 200, '计算成功');
        } catch (\Exception $exception) {
            return Responses::error([], 2001, '失败');
        }
    }

    /*
     * 房源后台列表渲染,多条件搜索
     * @name lak
     * */
    public function RoomList(Request $request)
    {
        try {
            $keyword = $request->all();
            $page = $request->get('page') ?: 1;
            $size = $request->get('size') ?: 10;
            $from = ($page - 1) * $size;
            //es多条件搜索
            if (!empty($keyword)) {

                if (isset($keyword['location']) && !empty($keyword['location'])) $query[] = ['match' => ['location' => $keyword['location']]];
                if (isset($keyword['region']) && !empty($keyword['region'])) $query[] = ['match' => ['region' => $keyword['region']]];


                RedisRankHistoryLogic::RedisRankHistory($query[0]['match']['location']);

                $field = 'location';
                $data = EsMultiple::EsMultipleSearch($query, $field, $from, $size);

                $data = json_decode(json_encode($data), true);
                return Responses::success($data, 200, '添加成功');
            }
            $res = ['id', 'location', 'region', 'price', 'updated_at', 'audit', 'area'];
            $room = Residential::GetPaginate($res);
            return Responses::success($room, 20000, '列表获取成功');
        } catch (\Exception $exception) {
            return Responses::error([], 2001, '失败');
        }
    }


    /*
     * 房源添加
     * @name lak
     * */
    public function RoomAdd(Request $request)
    {
        try {
            $data = $request->post();
            $request->validate([
                'location' => 'required|unique:fang_publish',
                'region' => 'required',
                'price' => 'required',
            ]);
            $room_data = RentingLogic::RentAdd($data);
            return Responses::success($room_data, 20000, '添加成功');
        } catch (\Exception $exception) {
            return Responses::error([], 2001, '失败');
        }
    }


    /*
     * 房源详情
     * @name lak
     * */
    public function RoomDetail(Request $request)
    {
        try {
            $room_id = $request->get('id');
            $room_data = RentingLogic::RentDetail($room_id);
            return Responses::success($room_data, 20000, '详情');
        } catch (\Exception $exception) {
            return Responses::error([], 2001, '失败');
        }
    }

    /*
     * 房源编辑展示
     * @name lak
     * */
    public function RoomShowUpdate(Request $request){
        try {
            $id = $request->get('id');
            $data = Residential::Getfirst(['id'=>$id]);
            return Responses::success($data, 20000, '编辑页面');
        } catch (\Exception $exception) {
            return Responses::error([], 2001, '失败');
        }

    }

    /*
     * 房源编辑
     * @name lak
     * */
    public function RoomUpdate(Request $request){
        try {
            $data = $request->input();
            $res = Residential::GetUpdate($data);
            return Responses::success($res, 20000, '编辑成功');
        } catch (\Exception $exception) {
            return Responses::error([], 2001, '失败');
        }

    }

    /*
     * 房源软删除
     * @name lak
     * */
    public function Roomdel(Request $request){
        try {
            $id = $request->get('id');
            Residential::destroy($id);
            return Responses::success([], 20000, '删除成功');
        } catch (\Exception $exception) {
            return Responses::error([], 2001, '失败');
        }

    }










    /*
    * 免费看房列表
    * @name lak
    * */
    public function RoomListFree(Request $request)
    {
        try {
            $data_free = Residential::GetPaginates();
            return Responses::success($data_free, 200, '列表展示成功');
        } catch (\Exception $exception) {
            return Responses::error([], 2001, '失败');
        }
    }

    /*
     * 免费看房详情
     * @name lak
     * */
    public function RoomDetailFree(Request $request)
    {
        try {
            $room_id = $request->get('id');
            $room_data = Residential::Getfirst(['id' => $room_id]);
            return Responses::success($room_data, 200, '列表展示成功');
        } catch (\Exception $exception) {
            return Responses::error([], 2001, '失败');
        }
    }

    /*
     * 预约看房
     * @name lak
     * */
    public function ReservationAdd(Request $request){
        try {
            $reservation_data = $request->post();
            $request->validate([
                'user_name' => 'required',
                'phone' => 'required',
                'reserve_address' => 'required',
            ]);
            $reservation_data = RentingLogic::ReservationAddLogic($reservation_data);
            return Responses::success($reservation_data, 200, '提交成功');
        } catch (\Exception $exception) {
            return Responses::error([], 2001, '失败');
        }

    }


    /*
     * 预约看房列表
     * @name lak
     * */
    public function ReservationList(Request $request){
        try {
            $Reservation_ist = Reservation::paginate(10);
            return Responses::success($Reservation_ist,200,'成功');
        }catch (\Exception $exception){
            return Responses::error([],2001,'失败');
        }
    }























    /**
     * 图片云存储
     * @lak
     */
    public function OssUploadFile(Request $request)
    {
        $filePath = $request->file('file')->getPathname();
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录https://ram.console.aliyun.com创建RAM账号。
        $accessKeyId = "LTAI5tE51TcDqNFXkixy21J6";
        $accessKeySecret = "lQz8kJwfXDXieuMiuhmAd2PZvL0Z6V";
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        $endpoint = "http://oss-cn-shanghai.aliyuncs.com";
        // 填写Bucket名称，例如examplebucket。
        $bucket = "hyzfw";
        // 上传到oss云存储新名字
        $ossName = 'house_resources/' . md5(rand(11111, 99999) . time()) . '.jpg';
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ossClient->uploadFile($bucket, $ossName, $filePath);
            $iconPath = "https://hyzfw.oss-cn-shanghai.aliyuncs.com/" . $ossName;
            return Responses::success($iconPath, 200, '上传成功');
        } catch (OssException $e) {
            return Responses::error([], 2001, '失败');
        }
    }


}
